﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TDT_Project.classes;
using Telerik.Web.UI;
using System.Data;
namespace TDT_Project.Controller
{
    public partial class XemDAKT_Main : System.Web.UI.UserControl
    {
        cls_phuhuynhxemdiem cls = new cls_phuhuynhxemdiem();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Session["nk"] != null)
                {
                    load_mon();
                    load_Rid();
                }
                else
                    Response.Redirect("Default.aspx");
            }

        }
        private void load_mon()
        {

            DataSet ds_mon = cls.Get_Mon();
            if (ds_mon != null && ds_mon.Tables[0].Rows.Count > 0)
            {
                cbo_monhoc.DataSource = ds_mon;
                cbo_monhoc.DataTextField = "TenMonHoc";
                cbo_monhoc.DataValueField = "MonHoc_id";
                cbo_monhoc.DataBind();
                cbo_monhoc.SelectedIndex = -1;
            }
        }
        private void load_Rid()
        {
            if (Session["nk"] != null)
            {
                string nk = Session["nk"].ToString();
                string Khoi = Session["Khoi"].ToString();
                DataSet ds_dakt = cls.Get_DAKT_Mon(nk, Khoi, "", 20, 1);
                if (ds_dakt != null && ds_dakt.Tables[0].Rows.Count > 0)
                {
                    rid_KT.DataSource = ds_dakt.Tables[0].DefaultView;
                    rid_KT.DataBind();
                    TotalRow = Convert.ToInt32(ds_dakt.Tables[0].Rows[0]["TotalRows"].ToString());


                    if ((TotalRow % PageSize) != 0)
                        TotalPage = (TotalRow / PageSize) + 1;
                    else
                        TotalPage = (TotalRow / PageSize);
                    ViewState["_TotalPage"] = TotalPage;

                    DLPaging.DataSource = BindPaging(PageSize, PageIndex, TotalPage);
                    DLPaging.DataBind();

                    if (TotalPage > 1)
                    {
                        divPaging.Attributes.Add("style", "display:block");
                        divPaging.Visible = true;
                    }
                    else
                    {
                        divPaging.Attributes.Add("style", "display:none");
                        divPaging.Visible = false;
                    }
                }
                else
                {
                    DataTable dt = new DataTable();
                    rid_KT.DataSource = dt;
                    rid_KT.DataBind();
                    divPaging.Visible = false;
                }
            }
            else
                Response.Redirect("Default.aspx");
        }
        private DataTable BindPaging(int PageSize, int CurrentPage, int TotalPage)
        {
            int FirstPage = 0;
            int LastPage = 0;
            DataTable dt = new DataTable();
            dt.Columns.Add("PageIndex");
            dt.Columns.Add("PageText");


            FirstPage = CurrentPage - 5;

            if (CurrentPage > 5)
                LastPage = CurrentPage + 4;
            else
                LastPage = 10;

            if (LastPage > TotalPage)
            {
                LastPage = TotalPage;
                FirstPage = LastPage - 10;
            }
            if (FirstPage <= 0)
                FirstPage = 1;

            for (int i = FirstPage; i <= LastPage; i++)
            {
                DataRow dr = dt.NewRow();
                dr[0] = i;
                dr[1] = i;
                dt.Rows.Add(dr);
            }
            return dt;
        }
        protected void cbo_monhoc_SelectedIndexChanged(object sender, Telerik.Web.UI.RadComboBoxSelectedIndexChangedEventArgs e)
        {
            if (Session["nk"] != null)
            {
                string nk = Session["nk"].ToString();
                string Khoi = Session["Khoi"].ToString();
                DataSet ds_dakt = cls.Get_DAKT_Mon(nk, Khoi, cbo_monhoc.SelectedValue.ToString(), 20, 1);
                if (ds_dakt != null && ds_dakt.Tables[0].Rows.Count > 0)
                {
                    rid_KT.DataSource = ds_dakt.Tables[0].DefaultView;
                    rid_KT.DataBind();
                    TotalRow = Convert.ToInt32(ds_dakt.Tables[0].Rows[0]["TotalRows"].ToString());


                    if ((TotalRow % PageSize) != 0)
                        TotalPage = (TotalRow / PageSize) + 1;
                    else
                        TotalPage = (TotalRow / PageSize);
                    ViewState["_TotalPage"] = TotalPage;

                    DLPaging.DataSource = BindPaging(PageSize, PageIndex, TotalPage);
                    DLPaging.DataBind();

                    if (TotalPage > 1)
                    {
                        divPaging.Attributes.Add("style", "display:block");
                        divPaging.Visible = true;
                    }
                    else
                    {
                        divPaging.Attributes.Add("style", "display:none");
                        divPaging.Visible = false;
                    }
                }
                else
                {
                    DataTable dt = new DataTable();
                    rid_KT.DataSource = dt;
                    rid_KT.DataBind();
                    divPaging.Visible = false;
                }
            }
            else
                Response.Redirect("Default.aspx");
        }

        #region "Paging Properties"

        private int TotalRow
        {
            set { ViewState["_TotalRow"] = value; }
            get
            {
                int TotalRow;
                if (ViewState["_TotalRow"] == null)
                {
                    TotalRow = 0;
                }
                else
                {
                    TotalRow = Convert.ToInt32(ViewState["_TotalRow"].ToString());
                }
                return TotalRow;
            }
        }
        private int TotalPage
        {
            set { ViewState["_TotalPage"] = value; }
            get
            {
                int TotalPg;
                if (ViewState["_TotalPage"] == null)
                {
                    TotalPg = 1;
                }
                else
                {
                    TotalPg = Convert.ToInt32(ViewState["_TotalPage"].ToString());
                }
                return TotalPg;
            }
        }
        private int PageSize
        {
            get
            {
                object objPage = ViewState["_PageSize"];
                int _PageSize = 20;
                if (objPage == null)
                {
                    _PageSize = 20;
                }
                else
                {
                    _PageSize = (int)objPage;
                }
                return _PageSize;
            }
            set { ViewState["_PageSize"] = value; }
        }
        private int PageIndex
        {
            get
            {
                int Index;
                if (ViewState["_PageIndex"] == null)
                {
                    Index = 1;
                }
                else
                {
                    Index = Convert.ToInt32(ViewState["_PageIndex"].ToString());
                }
                return Index;

            }
            set { ViewState["_PageIndex"] = value; }
        }
        #endregion
        #region phân trang
        protected void DLPaging_ItemCommand(object source, DataListCommandEventArgs e)
        {
            try
            {
                if (Session["nk"] != null)
                {
                    if (e.CommandName.Equals("Paging"))
                    {
                        PageIndex = Convert.ToInt32(e.CommandArgument.ToString());
                        //Warranty war = warranty();
                        PageSize = 20;
                        //LoadWarrantyList(war, PageSize);
                        string nk = Session["nk"].ToString();
                        string Khoi = Session["Khoi"].ToString();
                        DataSet ds_dakt = cls.Get_DAKT_Mon(nk, Khoi, cbo_monhoc.SelectedValue.ToString(), PageSize, PageIndex);
                        if (ds_dakt != null && ds_dakt.Tables[0].Rows.Count > 0)
                        {
                            rid_KT.DataSource = ds_dakt.Tables[0].DefaultView;
                            rid_KT.DataBind();
                            TotalRow = Convert.ToInt32(ds_dakt.Tables[0].Rows[0]["TotalRows"].ToString());


                            if ((TotalRow % PageSize) != 0)
                                TotalPage = (TotalRow / PageSize) + 1;
                            else
                                TotalPage = (TotalRow / PageSize);
                            ViewState["_TotalPage"] = TotalPage;

                            DLPaging.DataSource = BindPaging(PageSize, PageIndex, TotalPage);
                            DLPaging.DataBind();

                            if (TotalPage > 1)
                            {
                                divPaging.Attributes.Add("style", "display:block");
                                divPaging.Visible = true;
                            }
                            else
                            {
                                divPaging.Attributes.Add("style", "display:none");
                                divPaging.Visible = false;
                            }
                        }
                        else
                        {
                            DataTable dt = new DataTable();
                            rid_KT.DataSource = dt;
                            rid_KT.DataBind();
                            divPaging.Visible = false;
                        }
                    }
                }
                else
                    Response.Redirect("Default.aspx");
            }
            catch (Exception ex)
            {
                //Utils.WriteLogError("CustomerCarePlanList - DLPaging_ItemCommand - Username " + Session["UserName"].ToString(), ex.Message);
            }
        }
        protected void DLPaging_ItemDataBound(object sender, DataListItemEventArgs e)
        {
            try
            {
                LinkButton lnkbtnPage = (LinkButton)e.Item.FindControl("lnkbtnPaging");
                if (lnkbtnPage.CommandArgument.ToString() == PageIndex.ToString())
                {
                    lnkbtnPage.ForeColor = System.Drawing.Color.Red;
                    lnkbtnPage.Font.Bold = true;
                    lnkbtnPage.Text = "<span class=\"selected\">[" + lnkbtnPage.Text + "] </span>";
                }
            }
            catch (Exception ex)
            {
                // Utils.WriteLogError("WarrantyCard - DLPaging_ItemDataBound - Username " + Session["UserName"].ToString(), ex.Message);
            }
        }
        protected void lntLast_Click(object sender, EventArgs e)
        {
            if (Session["nk"] != null)
            {
                PageIndex = TotalPage;
                PageSize = 20;
                string nk = Session["nk"].ToString();
                string Khoi = Session["Khoi"].ToString();
                DataSet ds_dakt = cls.Get_DAKT_Mon(nk, Khoi, cbo_monhoc.SelectedValue.ToString(), PageSize, PageIndex);
                if (ds_dakt != null && ds_dakt.Tables[0].Rows.Count > 0)
                {
                    rid_KT.DataSource = ds_dakt.Tables[0].DefaultView;
                    rid_KT.DataBind();
                    TotalRow = Convert.ToInt32(ds_dakt.Tables[0].Rows[0]["TotalRows"].ToString());


                    if ((TotalRow % PageSize) != 0)
                        TotalPage = (TotalRow / PageSize) + 1;
                    else
                        TotalPage = (TotalRow / PageSize);
                    ViewState["_TotalPage"] = TotalPage;

                    DLPaging.DataSource = BindPaging(PageSize, PageIndex, TotalPage);
                    DLPaging.DataBind();

                    if (TotalPage > 1)
                    {
                        divPaging.Attributes.Add("style", "display:block");
                        divPaging.Visible = true;
                    }
                    else
                    {
                        divPaging.Attributes.Add("style", "display:none");
                        divPaging.Visible = false;
                    }
                }
                else
                {
                    DataTable dt = new DataTable();
                    rid_KT.DataSource = dt;
                    rid_KT.DataBind();
                    divPaging.Visible = false;
                }
                //Warranty war = warranty();
                //PageSize = Convert.ToInt32(Utils.KillChars(ddlNumPlansPerPage.SelectedValue.Trim()));
                //LoadWarrantyList(war, PageSize);
            }
            else
                Response.Redirect("Default.aspx");
        }
        protected void lntFirst_Click(object sender, EventArgs e)
        {
            if (Session["nk"] != null)
            {
                PageIndex = 1;
                PageSize = 20;
                string nk = Session["nk"].ToString();
                string Khoi = Session["Khoi"].ToString();
                DataSet ds_dakt = cls.Get_DAKT_Mon(nk, Khoi, cbo_monhoc.SelectedValue.ToString(), PageSize, PageIndex);
                if (ds_dakt != null && ds_dakt.Tables[0].Rows.Count > 0)
                {
                    rid_KT.DataSource = ds_dakt.Tables[0].DefaultView;
                    rid_KT.DataBind();
                    TotalRow = Convert.ToInt32(ds_dakt.Tables[0].Rows[0]["TotalRows"].ToString());


                    if ((TotalRow % PageSize) != 0)
                        TotalPage = (TotalRow / PageSize) + 1;
                    else
                        TotalPage = (TotalRow / PageSize);
                    ViewState["_TotalPage"] = TotalPage;

                    DLPaging.DataSource = BindPaging(PageSize, PageIndex, TotalPage);
                    DLPaging.DataBind();

                    if (TotalPage > 1)
                    {
                        divPaging.Attributes.Add("style", "display:block");
                        divPaging.Visible = true;
                    }
                    else
                    {
                        divPaging.Attributes.Add("style", "display:none");
                        divPaging.Visible = false;
                    }
                }
                else
                {
                    DataTable dt = new DataTable();
                    rid_KT.DataSource = dt;
                    rid_KT.DataBind();
                    divPaging.Visible = false;
                }
                //Warranty war = warranty();
                //PageSize = Convert.ToInt32(Utils.KillChars(ddlNumPlansPerPage.SelectedValue.Trim()));
                //LoadWarrantyList(war, PageSize);
            }
            else
                Response.Redirect("Default.aspx");
        }
        #endregion

        protected void rid_KT_ItemCommand(object sender, GridCommandEventArgs e)
        {
            try
            {
                string cmd = e.CommandName;
                int iRow = e.Item.ItemIndex;
                if (cmd == "TieuDe")
                {
                    Response.Redirect("XemDAKT.aspx?pages=XemKT_sub&ID=" + rid_KT.Items[iRow]["ID"].Text);

                }
            }
            catch (Exception)
            {

                throw;
            }
        }
    }
}